<?php
		session_start();
include_once ("annotation.php");
include_once ("mysqllib.php");
class annotationList {

	private $listAnnot;

	//construct from the id of the desired video.
	public function __construct($id) {
		$this -> getAllAnnotationsById($id);
	}

	public function printFormAddAnnotation($id) {
		echo '<form id="addAnnotationForm"  onSubmit="return addAnnotationCall(' . $id .','.$_SESSION['idUser']. ','.$_SESSION['status']. ');" method="POST">';
		echo '<p>Title : <input type="text" name="title" id="title"/></p>';
		echo '<p>Time : Current video time </p> ';
		echo '<p>Content : <textarea name="newContent" id="newContent" rows=3 cols=30 ></textarea></p>';
		echo '<p><input name="shared" id="shared" type="checkbox" checked="yes"/> shared</p>';
		if ($_SESSION['status'] == $GLOBALS['professor']) {
			echo '<p><input name="editable" id="editable" type="checkbox" checked="yes"/> editable</p>';
		}
		echo '<input type="submit"/>';
		echo '</form>';
	}

	//Display all the annotations currently set.
	public function displayAnnotationsForList($idVideo) {
		session_start();
		echo '<aside>';
		
		echo '<div id="accordion">';

		//The tab to add an annotation only if the user can edit
		if (!$_SESSION['blocked']) {
			echo '<h3 >Add an annotation</h3>';
			echo '<div id="addAnnotation">';
			//from for the new annotation
			$this -> printFormAddAnnotation($idVideo);
			echo '</div>';
		}
		//All the existing annotation's tabs
		if (!is_null($this -> listAnnot)) {

			foreach ($this->listAnnot as $annotation) {

				$annotation -> displayAnnotationForList();
			}
		} else {
			echo '<h3 >No annotation</h3> <div>Be the first to add an annotation to this Video</div>';
		}

		echo '</div>';
		echo '</aside>';
	}

	public function displayAnnotationsForPins($duration) {
		echo '<div id="pinsAnnot">';
		if (!is_null($this -> listAnnot)) {
			$id = 1;
			foreach ($this->listAnnot as $annotation) {

				$annotation -> displayAnnotationPin($duration, $id);
				$id++;
			}
		}

		echo '</div>';
	}

	//Fill the list of annotation with the annotations of the video with this $idVideo
	public function getAllAnnotationsById($idVideo) {
		//connect to the database.
		$db = new mysqllib();
		$id = $_SESSION['idUser'];
		$this -> listAnnot = array();

		//Create and send query to get the corresponding annotations
		$query = "SELECT * FROM Annotation WHERE idVideo='$idVideo' AND (shared='1' OR (shared='0' AND idAuthor='$id')) ORDER BY time ASC";
		$result = mysql_query($query);

		//fetch all the annotations
		while ($data = mysql_fetch_array($result)) {
			$annotation = new annotation();
			$annotation -> setAnnotation($data['idAnnotation'], $data['idAuthor'],  $data['title'], $data['ewikiPageName'], $data['idVideo'], $data['time'], $data['shared'], $data['editable']);
			$idAnnotation = $data['idAnnotation'];

			$this -> listAnnot[$idAnnotation] = $annotation;
		}

	}

}
?>